package com.wl.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@MapperScan("com.wl.mapper")
@Configuration
public class DynamicDataSourceConfig {
  
//    @Autowired
//    private DynamicDataSourceProperties dataSourceProperties;

    @Bean("master")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.master")
    public DataSource masterDataSource() {
        return DruidDataSourceBuilder.create().build();
    }  
  
    @Bean("slave")
    @ConfigurationProperties(prefix = "spring.datasource.dynamic.datasource.slave")  
    public DataSource slaveDataSource() {  
        return DruidDataSourceBuilder.create().build();  
    }

    @Bean("dataSource")
    public DataSource dynamicDataSource(@Qualifier("master") DataSource master,@Qualifier("slave") DataSource slave) {
        DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();  
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put("master",master);
        targetDataSources.put("slave", slave);
        dataSource.setDefaultTargetDataSource(masterDataSource());  
        dataSource.setTargetDataSources(targetDataSources);  
        return dataSource;  
    }

    //数据源事物
    @Bean
    @Primary
    public DataSourceTransactionManager dataSourceTransactionManager(@Qualifier("dataSource") DataSource dataSource){
        return new DataSourceTransactionManager(dataSource);
    }

    //mybatisplus配置文件
    @Bean
    @ConfigurationProperties(prefix = "mybatis-plus.configuration")
    public MybatisConfiguration mybatisConfiguration(){
        return new MybatisConfiguration();
    }

    @Bean  
    public SqlSessionFactory sqlSessionFactory(@Qualifier("dataSource") DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean=new MybatisSqlSessionFactoryBean();
        //设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource);
        //加载sql映射文件
//        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:com/wukangyou.mapper/*mapper.xml"));
        //自动填充策略
//        sqlSessionFactoryBean.setGlobalConfig(new GlobalConfig().setBanner(true).setMetaObjectHandler(new MyMetaObjectHandler()));
        //拦截器
        sqlSessionFactoryBean.setPlugins(mybatisPlusInterceptor());
        //mybatisplus配置文件
        sqlSessionFactoryBean.setConfiguration(mybatisConfiguration());



        // 其他MyBatis配置...  
        return sqlSessionFactoryBean.getObject();
    }

    //SqlSessionTemplate模板
    @Bean
    @Primary
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlSessionFactory") SqlSessionFactory sqlSessionFactory){
        return new SqlSessionTemplate(sqlSessionFactory);
    }


    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }
}